
import java.awt.Color;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import sun.awt.image.ToolkitImage;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author tony
 */
public class ProgAppsUI extends javax.swing.JFrame {

    /**
     * Creates new form ProgAppsUI
     */
    public ProgAppsUI() {
        initComponents();
        setIcon();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        listOfDevicesTxtArea = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        IPTxtArea = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        updateTxtArea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        progressTxtField = new javax.swing.JTextArea();
        installBtn = new javax.swing.JButton();
        rstrBtn = new javax.swing.JButton();
        updtBtn = new javax.swing.JButton();
        Exit = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("OXiGEN");
        setMaximumSize(new java.awt.Dimension(760, 460));
        setMinimumSize(new java.awt.Dimension(760, 460));
        setPreferredSize(new java.awt.Dimension(760, 460));
        setResizable(false);
        getContentPane().setLayout(null);

        jButton1.setText("Back Up");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(480, 380, 80, 20);

        listOfDevicesTxtArea.setEditable(false);
        listOfDevicesTxtArea.setColumns(20);
        listOfDevicesTxtArea.setRows(5);
        jScrollPane1.setViewportView(listOfDevicesTxtArea);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(10, 30, 350, 150);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("List Of Devices Connected");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 10, 350, 14);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Installation Progress");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(390, 10, 350, 14);

        IPTxtArea.setColumns(20);
        IPTxtArea.setRows(5);
        jScrollPane2.setViewportView(IPTxtArea);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(390, 30, 350, 150);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Currently Back Up Files");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 200, 350, 20);

        updateTxtArea.setEditable(false);
        updateTxtArea.setColumns(20);
        updateTxtArea.setRows(5);
        jScrollPane3.setViewportView(updateTxtArea);

        getContentPane().add(jScrollPane3);
        jScrollPane3.setBounds(10, 220, 350, 150);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Progress");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(390, 200, 350, 14);

        progressTxtField.setColumns(20);
        progressTxtField.setRows(5);
        jScrollPane4.setViewportView(progressTxtField);

        getContentPane().add(jScrollPane4);
        jScrollPane4.setBounds(390, 220, 350, 150);

        installBtn.setText("Install");
        installBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                installBtnActionPerformed(evt);
            }
        });
        getContentPane().add(installBtn);
        installBtn.setBounds(190, 380, 80, 20);

        rstrBtn.setText("Restore");
        rstrBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rstrBtnActionPerformed(evt);
            }
        });
        getContentPane().add(rstrBtn);
        rstrBtn.setBounds(280, 380, 80, 20);

        updtBtn.setText("Update");
        updtBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updtBtnActionPerformed(evt);
            }
        });
        getContentPane().add(updtBtn);
        updtBtn.setBounds(10, 380, 80, 20);

        Exit.setText("Exit");
        Exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ExitActionPerformed(evt);
            }
        });
        getContentPane().add(Exit);
        Exit.setBounds(660, 380, 80, 20);

        jButton2.setText("Devices");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(100, 380, 80, 20);

        jButton3.setText("Clear");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(570, 380, 80, 20);

        jButton4.setText("Delete");
        getContentPane().add(jButton4);
        jButton4.setBounds(390, 380, 80, 20);

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bg.jpg"))); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(0, 0, 840, 410);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    progressTxtField.append("Back Up Files.... \n");
        try {
            Process p = Runtime.getRuntime().exec("adb pull sdcard/backup C:/OXiGEN_backup");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line = null;
            
            while ((line=in.readLine()) != null) {
                progressTxtField.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }// TODO add your handling code here:
     progressTxtField.append("Back Up Done. \n");       // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
//        jTextArea1.setVisible(false);
//        jTextArea1.setOpaque(false);
//        jTextArea1.setBackground(new Color(0, 0, 0, 0));
//        jTextArea2.setVisible(false);
//        jTextArea2.setOpaque(false);
//        jTextArea2.setBackground(new Color(0, 0, 0, 0));
//        jTextArea3.setVisible(false);
//        jTextArea3.setOpaque(false);
//        jTextArea3.setBackground(new Color(0, 0, 0, 0));
        try {
            Process p = Runtime.getRuntime().exec("adb devices -l");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                listOfDevicesTxtArea.append(line + "\n");

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        listOfDevicesTxtArea.append("Listing devices done."+ "\n");// TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitActionPerformed
        System.exit(0);// TODO add your handling code here:
    }//GEN-LAST:event_ExitActionPerformed

    private void installBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_installBtnActionPerformed
        try {
            Process p = Runtime.getRuntime().exec("adb install /sasa.apk");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                IPTxtArea.append(line + "\n");

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        IPTxtArea.append("Installation Done. \n");// TODO add your handling code here:
    }//GEN-LAST:event_installBtnActionPerformed

    private void rstrBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rstrBtnActionPerformed
//        adb push C:/sample.txt sdcard/backup// TODO add your handling code here:
        progressTxtField.append("Restoring files.... \n");
        try {
            Process p = Runtime.getRuntime().exec("adb push C:/OXiGEN_backup sdcard/backup");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
                progressTxtField.append(line + "\n");

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        progressTxtField.append("Restore Done. \n");
    }//GEN-LAST:event_rstrBtnActionPerformed

    private void updtBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updtBtnActionPerformed
        String path = "C:/OXiGEN_backup/"; 
 
  String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles(); 
 
  for (int i = 0; i < listOfFiles.length; i++) 
  {
 
   if (listOfFiles[i].isFile()) 
   {
   files = listOfFiles[i].getName();
   updateTxtArea.append(files + "\n");
      }
  }
   updateTxtArea.append("Update Done. \n");
  // TODO add your handling code here:
    }//GEN-LAST:event_updtBtnActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
       IPTxtArea.setText(null);
       listOfDevicesTxtArea.setText(null);
       progressTxtField.setText(null);
       updateTxtArea.setText(null);
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ProgAppsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ProgAppsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ProgAppsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ProgAppsUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ProgAppsUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Exit;
    private javax.swing.JTextArea IPTxtArea;
    private javax.swing.JButton installBtn;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTextArea listOfDevicesTxtArea;
    private javax.swing.JTextArea progressTxtField;
    private javax.swing.JButton rstrBtn;
    private javax.swing.JTextArea updateTxtArea;
    private javax.swing.JButton updtBtn;
    // End of variables declaration//GEN-END:variables

    private void setIcon() {
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("ic_launcher.png")));
    }
}
